var Game=function(){

	//demo对象
	var gameDiv;
	var nextDiv;
	var timeDiv;
	var scoreDiv;
	var resultDiv;

	var score=0; //分数

	//将每一个20px*20px的方块映射成坐标里的一个个的点,方便操作
	//将这些坐标地址存储成数据
	var nextData=[
		[0,0,0,0],
		[0,0,0,0],
		[0,0,0,0],
		[0,0,0,0]
	];
	var gameData=[
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0,0,0]
	];

	var curSquare;
	var nextSquare;

	var nextDivs=[];
	var gameDivs=[];
	//初始化div
	var initDiv=function(container, data, divs){
		for(var i=0;i<data.length;i++){
			var div=[];
			for(var j=0;j<data[0].length;j++){
				var newNode=document.createElement('div');
				newNode.className='none';
				newNode.style.top=(i*20)+'px';
				newNode.style.left=(j*20)+'px';
				container.appendChild(newNode);
				div.push(newNode);
			}
			divs.push(div);
		}
	}
	//刷新div
	var refreshDiv=function(data, divs){
		for(var i=0;i<data.length;i++){
			for(var j=0;j<data[0].length;j++){
				if (data[i][j]==0) {
					divs[i][j].className="none";
				}else if(data[i][j]==1){
					divs[i][j].className="done";
				}else if(data[i][j]==2){
					divs[i][j].className="current";
				}
			}
		}
	}

	//检查点是否合法
	var check=function(pos, x, y){
		//x是行即纵坐标，y是列横坐标
		if(pos.x+x<0){
			return false; //超出上边界
		}else if(pos.x+x>=gameData.length){
			return false; //超出下边界
		}else if(pos.y+y<0){
			return false; //超出左边界
		}else if(pos.y+y>=gameData[0].length){
			return false; //超出右边界
		}else if(gameData[pos.x+x][pos.y+y]==1){
			return false; //俄罗斯方块游戏中之前已经落下的点(方块)
		}else{
			return true;
		}
	}

	//设置数据
	var setData=function(){
		for (var i = 0; i < curSquare.data.length; i++) {
			for (var j = 0; j < curSquare.data[0].length; j++) {
				if(check(curSquare.origin,i,j)){
					gameData[curSquare.origin.x+i][curSquare.origin.y+j]=curSquare.data[i][j];
				}
			}
		}
	}

	//检测数据是否合法
	var isValid=function(pos, data){
		for(var i=0;i<data.length;i++){
			for(var j=0;j<data[0].length;j++){
				if(data[i][j]!=0){
					if(!check(pos,i,j)){
						return false;
					}
				}
			}
		}
		return true;
	}

	//清除数据
	var clearData=function(){
		for (var i = 0; i < curSquare.data.length; i++) {
			for (var j = 0; j < curSquare.data[0].length; j++) {
				if(check(curSquare.origin,i,j)){
					gameData[curSquare.origin.x+i][curSquare.origin.y+j]=0;
				}
			}
		}
	}

	//下移
	var down=function(){
		if(curSquare.canDown(isValid)){
			clearData();
			curSquare.down();  //方块数据变化
			setData();
			refreshDiv(gameData, gameDivs);

			return true;  //给个返回值，若能再向下则返回true,
			//给后面的降落函数使用。
		}else{
			return false;  //给个返回值，若能不再向下则返回false,
		}
	}

	//降落
	var fall=function(){
		while(down()); //若true，继续不断执行down()
	}

	//左移
	var left=function(){
		if(curSquare.canLeft(isValid)){
			clearData();
			curSquare.left();  //方块数据变化
			setData();
			refreshDiv(gameData, gameDivs);
		}
	}

	//右移
	var right=function(){
		if(curSquare.canRight(isValid)){
			clearData();
			curSquare.right();  //方块数据变化
			setData();
			refreshDiv(gameData, gameDivs);
		}
	}

	//旋转变化
	var rotate=function(){
		if(curSquare.canRotate(isValid)){
			clearData();
			curSquare.rotate();  //方块数据变化
			setData();
			refreshDiv(gameData, gameDivs);
		}
	}

	//到底部时固定方块
	var fixed=function(){
		for (var i = 0; i < curSquare.data.length; i++) {
			for (var j = 0; j < curSquare.data[0].length; j++) {
				if (check(curSquare.origin, i, j)) {
					if (gameData[curSquare.origin.x+i][curSquare.origin.y+j]==2) {
						gameData[curSquare.origin.x+i][curSquare.origin.y+j]=1;
					}
				}
			}
		}
		refreshDiv(gameData, gameDivs);
	}

	//使用下一个方块
	var performNext=function(type, dir){
		curSquare=nextSquare;
		setData();
		nextSquare=SquareFactory.prototype.make(type, dir);
		refreshDiv(gameData, gameDivs);
		refreshDiv(nextSquare.data, nextDivs);
	}

	//消行
	var checkClear=function(){

		//消行时计算此次消掉的行数：
		var line=0;

		//“从最底行开始”遍历gameData判断是否可消行并执行消行
		for (var i = gameData.length - 1; i >= 0; i--) {
			//判断i行是不是可以消除，将结果赋予clear
			var clear = true;
			for (var j = 0; j < gameData[0].length; j++) {
				if (gameData[i][j] != 1) {
					clear=false;
					break;
				}
			}
			//消除
			if (clear) {

				line=line+1;

				for (var m = i; m > 0; m--) {
					for(var n = 0; n < gameData[0].length; n++){
						gameData[m][n] = gameData[m-1][n];
						//因为消除一行，所有当前所有的方块纵坐标向下移动一行
						//消一行==当前所有的方块向下移动一行
					}
				}
				//新的第一行初始化为0?
				for(var n = 0; n < gameData[0].length; n++){
						gameData[0][n] = 0;
				}
				i++;  //这里i指向最顶层for的i
			}
		}
		return line; //返回消掉的函数
	}

	//设置时间
	var setTime=function(time){
		timeDiv.innerHTML=time;   
	}

	//设置分数
	var addScore=function(line){
		var s=0;
		switch(line){
			case 1:
				s=10;
				break;
			case 2:
				s=30;
				break;
			case 3:
				s=60;
				break;
			case 4:
				s=100;
				break;
			default:
				break;
		}
		score=score+s;
		scoreDiv.innerHTML=score;
	}

	//玩家交互对抗性：底部增加干扰的行（方块）
	var addTailLines=function(lines){ //lines:生成干扰行并返回增加的行数

		for (var i = 0; i < gameData.length - lines.length; i++) {
			gameData[i]=gameData[i+lines.length];
		} //所有的方块向上移动lines.length行
		for (var i = 0; i < lines.length; i++) {
			gameData[gameData.length-lines.length+i]=lines[i];
		} //底部增加lines.length行干扰行

		//当前方块的位置也要向上移动lines.length行
		curSquare.origin.x=curSquare.origin.x-lines.length;
		//如果当前的方块已经在第一行，则不变
		if (curSquare.origin.x<0) { //当前方块减完后为负数
			curSquare.origin.x=0; //则让它在第一行(不变)
		}

		refreshDiv(gameData, gameDivs);
	}

	//检查游戏结束
	var checkGameOver=function(){
		var gameOver = false;
		for (var i=0; i<gameData[0].length; i++) {
			if (gameData[1][i] == 1) {
			//俄罗斯方块游戏中要是第二行有方块固定(==1)了,
			//那游戏不可能继续了，肯定是满了，这是游戏规律
				gameOver = true;
			}
		}
		return gameOver;
	}

	//游戏结束后在界面中显示游戏结果的函数
	var gameover=function(result){
		if (result) {
			resultDiv.innerHTML="你赢了";
		} else {
			resultDiv.innerHTML="你输了";
		}
	}


	//初始化界面
	var init=function(doms, type, dir){

		gameDiv=doms.gameDiv;
		nextDiv=doms.nextDiv;
		timeDiv=doms.timeDiv;
		scoreDiv=doms.scoreDiv;
		resultDiv=doms.resultDiv;

		nextSquare=SquareFactory.prototype.make(type, dir)
		//在local.js的start()中生成随机的方块
		//而start()又调用game即这里的performNext()
		//使curSquare=nextSquare
		initDiv(gameDiv, gameData, gameDivs);
		initDiv(nextDiv, nextSquare.data, nextDivs);

		refreshDiv(nextSquare.data, nextDivs);
	}

	//导出API
	this.init=init;
	this.down=down;
	this.left=left;
	this.right=right;
	this.rotate=rotate;
	this.fall=fall;
	this.fixed=fixed;
	this.performNext=performNext;
	this.checkClear=checkClear;
	this.checkGameOver=checkGameOver;
	this.setTime=setTime;
	this.addScore=addScore;
	this.gameover=gameover;
	this.addTailLines=addTailLines;
}



